/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.mes.base.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.mes.base.entity.ProduceSite;
import org.springblade.modules.mes.base.service.IProduceSiteService;
import org.springblade.modules.mes.base.vo.ProduceSiteVO;
import org.springblade.modules.mes.opcua.constant.OpcUaCache;
import org.springblade.modules.system.util.QueryParaUtil;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 生产站位 控制器
 *
 * @author BladeX
 * @since 2020-09-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("/produceSite")
@Api(value = "生产站位", tags = "生产站位接口")
public class ProduceSiteController extends BladeController {

	private final IProduceSiteService produceSiteService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	//@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入produceSite")
	public R<ProduceSite> detail(ProduceSite produceSite) {
		ProduceSite detail = produceSiteService.getOne(Condition.getQueryWrapper(produceSite));
		return R.data(detail);
	}

	/**
	 * 不分页 生产站位
	 */
	@GetMapping("/list")
	//@ApiOperationSupport(order = 2)
	@ApiOperation(value = "不分页", notes = "传入produceLine")
	public R<List<ProduceSite>> list(ProduceSiteVO produceSiteVO) {
		ProduceSiteVO cond = buildCond(produceSiteVO);
		List<ProduceSite> produceLineVOList = produceSiteService.list(Condition.getQueryWrapper(cond));
		return R.data(produceLineVOList);
	}


	/**
	 * 自定义分页 生产站位
	 */
	@GetMapping("/page")
	//@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入produceSite")
	public R<IPage<ProduceSiteVO>> page(ProduceSiteVO produceSiteVO, Query query) {
		ProduceSiteVO cond = buildCond(produceSiteVO);
		IPage<ProduceSiteVO> pages = produceSiteService.selectProduceSitePage(Condition.getPage(query), cond);
		return R.data(pages);
	}


	/**
	 * 新增 生产站位
	 */
	@PostMapping("/save")
	//@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入produceSite")
	public R save(@Valid @RequestBody ProduceSite produceSite) {
		try {
			long count = produceSiteService.count(new QueryWrapper<ProduceSite>().eq("site_code", produceSite.getSiteCode()));
			if (count > 0) {
				return R.fail("生产站位编码已存在，请重新编辑！");
			}
			boolean flag = produceSiteService.save(produceSite);

			return R.status(flag);
		} finally {
			OpcUaCache.clear();
		}
	}


	/**
	 * 新增或修改
	 * （前端修改用的这个接口）
	 */
	@PostMapping("/submit")
	@ApiOperation(value = "新增或修改", notes = "传入produceSite")
	public R submit(@Valid @RequestBody ProduceSite produceSite) {
		try {
			long count = produceSiteService.count(new QueryWrapper<ProduceSite>().eq("site_code", produceSite.getSiteCode())
				.ne("id", produceSite.getId()));
			if (count > 0) {
				return R.fail("生产站位编码已存在，请重新编辑！");
			}
			boolean flag = produceSiteService.updateById(produceSite);
			return R.status(flag);
		} finally {
			OpcUaCache.clear();
		}
	}

	/**
	 * 新增或修改
	 * （前端修改用的这个接口）
	 */
	@PostMapping("/add")
	@ApiOperation(value = "新增", notes = "传入produceSite")
	public R add(@Valid @RequestBody ProduceSite produceSite) {
		try {
			long count = produceSiteService.count(new QueryWrapper<ProduceSite>().eq("site_code", produceSite.getSiteCode()));
			if (count > 0) {
				return R.fail("生产站位编码已存在，请重新编辑！");
			}
			produceSite.setId(IdUtil.getSnowflake(1, 2).nextId());
			boolean flag = produceSiteService.save(produceSite);
			return R.status(flag);
		} finally {
			OpcUaCache.clear();
		}
	}

	/**
	 * 新增或修改
	 * （前端修改用的这个接口）
	 */
	@PostMapping("/edit")
	@ApiOperation(value = "新增或修改", notes = "传入produceSite")
	public R edit(@Valid @RequestBody ProduceSite produceSite) {
		try {
			long count = produceSiteService.count(new QueryWrapper<ProduceSite>().eq("site_code", produceSite.getSiteCode())
				.ne("id", produceSite.getId()));
			if (count > 0) {
				return R.fail("生产站位编码已存在，请重新编辑！");
			}
			boolean flag = produceSiteService.updateById(produceSite);
			return R.status(flag);
		} finally {
			OpcUaCache.clear();
		}
	}

	/**
	 * 删除 生产站位
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		try {
			List<Long> idList = Func.toLongList(ids);
			ProduceSiteVO produceSite = new ProduceSiteVO();
			for (Long id : idList) {
				produceSite.setId(id);
				produceSiteService.deleteById(produceSite);
			}
			return R.status(true);
		} finally {
			OpcUaCache.clear();
		}
	}

	@NotNull
	private ProduceSiteVO buildCond(ProduceSiteVO produceSiteVO) {
		return QueryParaUtil.trim(produceSiteVO);
	}

}
